package com.magesti.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.magesti.DAO.DaoFactory;
import com.magesti.DAOinterface.CalendrierInterfaceDAO;
import com.magesti.DAOinterface.OperationInterfaceDAO;
import com.magesti.beans.Operation;
import com.magesti.exception.DAOException;
import com.magesti.ressource.Global;
import com.magesti.ressource.ServletGlobal;

@SuppressWarnings("serial")
public class AfficheOperation extends HttpServlet {

	private OperationInterfaceDAO operationDAO;
	private CalendrierInterfaceDAO calendrierDAO;

	public void init() throws ServletException {
		DaoFactory f = ((DaoFactory) getServletContext().getAttribute(
				Global.ATT_CONTEXT_DAO_FACTORY));
		operationDAO = f.getOperationDAO();
		calendrierDAO = f.getCalendrierDAO();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int id_op = Global.VALEUR_ID_DEFAUT;
		Operation op = null;
		String erreur = "";

		try {
			id_op = ServletGlobal.checkIdOperation(request);
			op = operationDAO.createOperation(id_op);

			op.setCalendrier(calendrierDAO.addCompleteDefautCalendrier(op
					.getCalendrier()));
			// TODO: faire un formGet pour lorque ce sont just des requetes get
		} catch (DAOException e) {
			e.printStackTrace();
		}

		request.setAttribute(Global.ATT_REQUEST_OPERATION, op);
		request.setAttribute(Global.ATT_REQUEST_ERREUR, erreur);

		this.getServletContext()
				.getRequestDispatcher(Global.VUE_JSP_AFFICHE_OPERATION)
				.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// this.getServletContext().getRequestDispatcher(VUE_EXEMPLE).forward(request,response);
		// response.sendRedirect(request.getContextPath() + VUE_EXEMPLE);
	}

}
